#!/bin/bash

if [[ "$(which clang-format)" == "" ]]; then
  echo "Please install clang-format on your OS!"
fi

usage () {
  echo "USAGE: format2google [options] <file>"
  echo "       format2google [options] <dir>"
  echo
  echo "OPTIONS:"
  echo "      -h, --help   Print usage"
  echo
}

if [[ $# -eq 1 ]]; then
  case "${1}" in
      -h | --help)
          usage
          exit 0
          ;;
      * )
          input=${1}
          ;;
  esac
else
  usage
  exit 1
fi

file_list=$(find ${input} -name "*.cc" -or -name "*.cpp" \
  -or -name "*.mlu" -or -name "*.h" -or -name "*.hpp")
mluop_name="mlu_op.h"
for file in ${file_list}; do
  echo "clang-formating ${file} ..."
  if [[ $file == *${mluop_name} ]]; then
    clang-format -style="{BasedOnStyle: google,AlwaysBreakAfterReturnType: TopLevel, \
      BinPackParameters: false, AlignConsecutiveAssignments: true, ColumnLimit: 120 }" -i ${file}
  else
    clang-format -style="{BasedOnStyle: google, PointerAlignment: Right, \
      SortIncludes: false, ColumnLimit: 80}" -i ${file}
  fi
done
